package org.corey.share.business;

import java.util.Date;
import java.util.UUID;

import org.corey.share.db.entity.CoreyFile;
import org.corey.share.db.service.CoreyFileService;
import org.corey.share.db.service.CoreyFileServiceImpl;
import org.corey.share.misc.UploadAction;

@SuppressWarnings("serial")
public class FileUploadAction extends UploadAction {
	private CoreyFileService service = new CoreyFileServiceImpl();

	/**
	 * 保存文件，并且保存信息如下：{files:[{fileName:
	 * 'a.jpg',fileSize:12,fileId:2}..],respository:'aaaa',count:4}
	 */
	public String execute() {
		String filesInfo = "[";
		if (this.getFiles() != null) {
			int index=0;
			for (CoreyFile file : this.getFiles()) {
				index++;
				file.setRespositoty(this.getRespository());
				file.setUser(this.getUser());
				file.setUploadDate(new Date());
				file.setServerSideFileName(UUID.randomUUID().toString().replace("-", ""));
				try {
					service.uploadFile(file);
				} catch (Exception e) {
					e.printStackTrace();
					continue;
				}
				String fileInfo=file.getJsonInfo();
				filesInfo+=fileInfo;
				if(index<this.getFiles().size()){
					filesInfo+=",";
				}
			}
			filesInfo+="]";
		}
		return filesInfo;
	}
}
